<?php


class Test_ExtjsController extends WSB_Controller_Action
{

	public function init()
	{
		parent::init();

		$navigation = $this->_helper->getHelper('Navigation');
		$navigation->add(WSB_ADMIN_URL . 'test/index/', 'Index');
		$navigation->add(WSB_ADMIN_URL . 'test/extjs/', 'EXT-JS');
		$navigation->add(WSB_ADMIN_URL . 'test/yui/', 'YUI');
		$navigation->add(WSB_ADMIN_URL . 'test/dojo/', 'Dojo');

	}

	public function preDispatch()
	{
        $this->_response->appendBody('<h1>Ext JS Spielereien</h1>');
        $this->_response->appendBody('<ul style="list-style-type:none;">');
        $this->_response->appendBody('<li><a href="javascript:loadIntoContent(\'' . WSB_ADMIN_URL . 'test/extjs/dialog/\')">Dialog</a></li>');
        $this->_response->appendBody('<li><a href="javascript:loadIntoContent(\'' . WSB_ADMIN_URL . 'test/extjs/grid/\')">Table</a></li>');
        $this->_response->appendBody('<li><a href="javascript:loadIntoContent(\'' . WSB_ADMIN_URL . 'test/extjs/tree/\')">Tree</a></li>');
        $this->_response->appendBody('</ul>');
	}

	public function indexAction()
	{
	}

	public function dialogAction()
	{
		// Ist ein AJAX Request
 		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
			echo $this->view->render('extjs/dialog.phtml');
			exit;
		} else {
			$this->_helper->Redirect(WSB_ADMIN_URL . 'test/extjs/index/');
		}
	}

	public function gridAction()
	{
		$delete = intval($this->_getParam('delete'));
		if(!empty($delete)) {
			$this->view->error = sprintf('<div class="errors">Der Eintrag mit der ID %s wurde gel&ouml;scht.</div>', $delete);
		}
		// Ist ein AJAX Request
 		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
			echo $this->view->render('extjs/grid.phtml');
			exit;
		} else {
			$this->_helper->Redirect(WSB_ADMIN_URL . 'test/extjs/index/');
		}
	}

	public function treeAction()
	{
		// Ist ein AJAX Request
 		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
			echo $this->view->render('extjs/tree.phtml');
			exit;
		} else {
			$this->_helper->Redirect(WSB_ADMIN_URL . 'test/extjs/index/');
		}
	}

	public function subnavAction()
	{
		$this->_helper->viewRenderer->setNoRender();
 		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
			echo $this->_response->getBody();
		}
		exit;
	}

	public function ajaxXmlListAction()
	{
		header('Content-type: text/xml');
		$this->_helper->viewRenderer->setNoRender();
		$table = new Cms_Model_DbTable_Xtension();
		$collection = $table->fetchAll();
		echo $collection->toXml();
		exit;
	}

	public function jsonListAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$table = new Cms_Model_DbTable_Page();
		$collection = $table->fetchAll();
		echo Zend_Json::encode($collection->toArray());
		exit;
	}

	public function jsonTreeAction()
	{
		//header('Content-type: text/html');
		/*
		[text] => adapter
		[id] => /adapter
        [cls] => folder/file
        [leaf] => true/flase
        */
		$pid = intval($this->_getParam('node'));
		$branch = WSB_Node_Page::getTree();
		if($pid > 0) {
			$branch = $branch->getBranch($pid);
		}
		$pages = array();
		foreach($branch->getChildren() AS $child) {
			$cls = $child->hasChildren() ? 'folder' : 'file';
			$leaf = ($cls == 'file') ? true : false;
			$pages[] = array(
				'text' => $child->title,
				'id' => $child->id,
				'cls' => $cls,
				'leaf' => $leaf
			);
		}
		echo Zend_Json::encode($pages);
		exit;
	}

}
